/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fase2.pt.iul.sid.database;

import fase2.pt.iul.sid.model.EstabelecimentoModel;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Ricardo Marques
 */
public class EstabelecimentoDAO {
    
    private FactoryConnection fc = null;
    
    public EstabelecimentoDAO(FactoryConnection fc){
        this.fc = fc;
    }
    
    public EstabelecimentoModel getEstabelecimento(int id) {
      EstabelecimentoModel estabelecimento = null;

        int resultsetType =0;
        ConnectionHandler ch = new ConnectionHandler();
                
        try {
            fc.returnResultSetSelectStatement("SELECT * FROM Estabelecimento WHERE idEstabelecimento=" + id + " ", resultsetType, ch);

            if(ch.r!=null){
                if(ch.r.next()){

                    estabelecimento = new EstabelecimentoModel(ch.r.getString("informacoesHorario"), ch.r.getString("formaDeLaChegar"), ch.r.getString("coordenadasGps"),
                                        ch.r.getString("morada"),ch.r.getInt("idEstabelecimento"),ch.r.getString("designacao"), ch.r.getDouble("rating"));
                    
                }
            }
            
        } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        return estabelecimento;
      
    }
    public List<EstabelecimentoModel> getEstabelecimentos(){

        List<EstabelecimentoModel> estabelecimentos = new LinkedList<>();
        int resultsetType =0;
        ConnectionHandler ch = new ConnectionHandler();
                
        try {
            fc.returnResultSetSelectStatement("SELECT * FROM Estabelecimento", resultsetType, ch);

            if(ch.r!=null){
                                
                estabelecimentos.add(new EstabelecimentoModel("", "", "", "", 0, "Todos", 0.0));
                while(ch.r.next()){
    
             EstabelecimentoModel estabelecimento = new EstabelecimentoModel(ch.r.getString("informacoesHorario"), ch.r.getString("formaDeLaChegar"), ch.r.getString("coordenadasGps"),
                                        ch.r.getString("morada"),ch.r.getInt("idEstabelecimento"),ch.r.getString("designacao"), ch.r.getDouble("rating"));
                    
                    estabelecimentos.add(estabelecimento);
                    
                }
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return estabelecimentos;
      
   }
    
}
